package study.stack;

public class ArrayStack2 {
    private int maxSize;
    private int top = -1;
    private int[] stack;

    ArrayStack2(int maxSize){
        this.maxSize = maxSize;
        this.stack = new int[maxSize];
    }

    //桟满
    public boolean isFull(){
        return top == maxSize - 1;
    }

    //桟空
    public boolean isEmpty(){
        return top == -1;
    }

    //入栈
    public void push(int data){
        if (isFull()){
            System.out.println("桟满，无法入栈！");
            return;
        }
        top++;
        stack[top] = data;

    }

    //出栈
    public int pop(){
        if (isEmpty()){
            throw new RuntimeException("桟空");
        }
        int value = stack[top];
        top--;
        return value;
    }

    //遍历
    public void print(){
        for (int i = top; i >= 0 ; i--) {
            System.out.printf("stack[%d] = %d\n", i, stack[i]);
        }
    }

    //返回运算符的优先级
    public int getPriority(int oper){
        if (oper == '*' || oper == '/'){
            return 1;
        }else if (oper == '+' || oper == '-'){
            return 0;
        }else {
            return -1;
        }
    }

    //判断是不是运算符
    public boolean isOper(int value){
        return value == '*' || value == '/' || value == '+' || value == '-';
    }

    //计算方法
    public int calculate(int num1, int num2, int oper){
        int result = 0;
        switch(oper){
            case '+':
                result = num1 + num2;
                break;
            case '-':
                result = num2 - num1;
                break;
            case '*':
                result = num1 * num2;
                break;
            case '/':
                result = num2 / num1;
                break;
            default:
                break;
        }
        return result;
    }

    //获取栈顶元素
    public int peek(){
        if (isEmpty()){
            throw  new RuntimeException("桟为空");
        }
        return stack[top];
    }
}
